﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using theeg.DependencyAnalysis.CommonAddin.Behaviors;

namespace UnitTests.Addin
{
    [TestClass]
    public class HistoryTest
    {
        private History _history;

        [TestInitialize]
        public void Initialize()
        {
            _history = new History { Size = 3 };
        }

        [TestMethod]
        public void WhenItemIsChangedThenBackIsEnabled()
        {
            _history.CurrentItem = 1;
            Assert.IsTrue(_history.BackCanExecute());
        }

        [TestMethod]
        public void WhenBackIsPerformedThenForeIsEnabled()
        {
            _history.CurrentItem = 1;
            _history.BackExecuted();
            Assert.IsTrue(_history.ForeCanExecute());
        }

        [TestMethod]
        public void WhenItemHasNotChangedThenBackIsDisabled()
        {
            Assert.IsFalse(_history.BackCanExecute());
        }

        [TestMethod]
        public void WhenBackWasNotCalledThenForeIsDisabled()
        {
            _history.CurrentItem = 1;
            Assert.IsFalse(_history.ForeCanExecute());
        }

        [TestMethod]
        public void WhenLatestItemIsSelectedThenForeIsDisabled()
        {
            _history.CurrentItem = 1;
            _history.BackExecuted();
            _history.ForeExecuted();
            Assert.IsFalse(_history.ForeCanExecute());
        }

        [TestMethod]
        public void WhenSizeIsChangedThenHistoryIsReset()
        {
            _history.CurrentItem = 1;
            _history.CurrentItem = 2;
            _history.BackExecuted();
            _history.Size = 5;
            Assert.IsFalse(_history.BackCanExecute() || _history.ForeCanExecute());
        }

        [TestMethod]
        public void WhenBackIsPerformedThenCurrentItemIsPreviousOne()
        {
            _history.CurrentItem = 1;
            _history.CurrentItem = 2;
            _history.BackExecuted();
            Assert.AreEqual(1, _history.CurrentItem);
        }

        [TestMethod]
        public void WhenForeIsPerformedThenCurrentItemIsNextOne()
        {
            _history.CurrentItem = 1;
            _history.CurrentItem = 2;
            _history.BackExecuted();
            _history.ForeExecuted();
            Assert.AreEqual(2, _history.CurrentItem);
        }

        [TestMethod]
        public void WhenCapacityIsCompletelyUsedThenPurgeOldItems()
        {
            _history.CurrentItem = 1;
            _history.CurrentItem = 2;
            _history.CurrentItem = 3;
            _history.CurrentItem = 4;
            _history.BackExecuted();
            _history.BackExecuted();
            _history.BackExecuted();
            Assert.IsFalse(_history.BackCanExecute());
        }

        [TestMethod]
        public void WhenCurrentItemIsSetThenForwardItemsArePurged()
        {
            _history.CurrentItem = 1;
            _history.CurrentItem = 2;
            _history.CurrentItem = 3;
            _history.CurrentItem = 4;
            _history.BackExecuted();
            _history.BackExecuted();
            _history.CurrentItem = 5;
            Assert.IsFalse(_history.ForeCanExecute());
        }
    }
}
